package com.young.msg.core;

import com.young.common.util.StringUtils;
import com.young.msg.model.MailSenderConfig;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * 邮件发送器
 *
 * @author wangjy
 * @date 2021/10/30 00:42:44
 */
public class YoungJavaMailSender extends JavaMailSenderImpl {
    /**
     * ssl邮件端口号
     * 默认端口25不安全, 推荐使用465
     */
    private static final Integer MAIL_SSL_PORT = 465;

    /**
     * 发送邮箱标识
     */
    private String senderCode;
    /**
     * 发件人名字
     */
    private String fromName;

    /**
     * 创建发送器实例
     *
     * @param mailConfig
     * @return
     */
    public static YoungJavaMailSender newInstance(MailSenderConfig mailConfig) {
        YoungJavaMailSender sender = new YoungJavaMailSender();
        sender.setHost(mailConfig.getMailHost());
        if (mailConfig.getMailPort() != null) {
            sender.setPort(mailConfig.getMailPort());
        }
        sender.setUsername(mailConfig.getUsername());
        sender.setPassword(mailConfig.getPassword());
        sender.setProtocol(mailConfig.getProtocol());
        sender.setDefaultEncoding(StandardCharsets.UTF_8.name());
        sender.senderCode = mailConfig.getSenderCode();
        sender.fromName = mailConfig.getFromName();
        // 加密传输
        if (MAIL_SSL_PORT.equals(mailConfig.getMailPort())) {
            Properties properties = new Properties();
            properties.setProperty("mail.smtp.auth", "true");
            properties.setProperty("mail.smtp.timeout", "5000");
            properties.setProperty("mail.smtp.starttls.enable", "true");
            properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            properties.setProperty("mail.smtp.socketFactory.fallback", "false");
            properties.setProperty("mail.smtp.ssl.enable", "true");
            sender.setJavaMailProperties(properties);
        }
        return sender;
    }

//    public static void main(String[] args) {
//        MailSenderConfig mailConfig = new MailSenderConfig();
//        mailConfig.setMailHost("smtp.163.com");
//        mailConfig.setMailPort(465);
//        mailConfig.setUsername("18235146426@163.com");
//        mailConfig.setPassword("RZFKWSEGRFTMXALX");
//        mailConfig.setProtocol("smtp");
//        mailConfig.setFromName("ceshi_name");
//        YoungJavaMailSender sender = YoungJavaMailSender.newInstance(mailConfig);
//
//        // 构造邮件发送命令对象
//        MailSendCommand command = MailSendCommand.build("", "243144837@qq.com", "465", "465");
//
//        // 构建邮件消息对象
//        SimpleMailMessage message = MailSenderControlCenter.buildMailMessage(command, sender);
//
//        // 发送
//        sender.send(message);
//
//    }

    /**
     * 校验发送器是否有效
     *
     * @return
     */
    public boolean check() {
        return StringUtils.isNotBlank(senderCode)
                && StringUtils.isNotBlank(this.getHost())
                && StringUtils.isNotBlank(this.getUsername())
                && StringUtils.isNotBlank(this.getPassword())
                && StringUtils.isNotBlank(this.getProtocol())
                && StringUtils.isNotBlank(this.getDefaultEncoding());
    }

    private YoungJavaMailSender() {
    }

    public String getSenderCode() {
        return senderCode;
    }

    public String getFromName() {
        return fromName;
    }
}
